﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.SanXuat.UTI;
using ThaiHoa.Win.QLSX.DAL;

namespace ThaiHoa.Win.QLSX.SanXuat.UI
{
    public partial class frmCongNhanSearch : Form
    {
        #region Fields
        KhoDBContext db;
        public event EventHandler<SearchEventArgs> Searched;
        List<LoaiCongNhan> listChucVu;
        #endregion
        #region Constructor
        public frmCongNhanSearch()
        {
            InitializeComponent();
        }
        #endregion
        #region Functions
        private void KhoiTao()
        {
            db = new KhoDBContext();
            listChucVu = new List<LoaiCongNhan>();
            listChucVu.Add(new LoaiCongNhan() { TenLoai = "Tất Cả" });
            listChucVu.AddRange(db.LoaiCongNhan.Where(l => l.TrangThai).ToList());
            cbChucVu.DataSource = listChucVu;

            //Định dạng control
            gbNangCao.Height = 30;
            this.Height = 140;
            txtTen.Focus();
        }
        #endregion
        #region Events
        private void chkNangCao_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNangCao.Checked)
            {
                gbNangCao.Height = 140;
                this.Height = 260;
            }
            else
            {
                gbNangCao.Height = 30;
                this.Height = 140;
            }
        }
        #endregion

        private void frmCongNhanSearch_Load(object sender, EventArgs e)
        {
            KhoiTao();
            
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (Searched != null)
            { 
                string ten=txtTen.Text;
                LoaiCongNhan loaiCongNhan=cbChucVu.SelectedItem as LoaiCongNhan;
                int namSinh=0;
                if (txtNamSinh.Text != "")
                {
                    if (!int.TryParse(txtNamSinh.Text, out namSinh))
                    {
                        Common.ShowMessengerError("Năm sinh không hợp lệ");
                        return;
                    }
                    if (namSinh < 1900 || namSinh > DateTime.Now.Year)
                    {
                        Common.ShowMessengerError("Năm sinh không hợp lệ");
                        return;
                    }
                }
                string diaChi=txtDiaChi.Text;
                string dienThoai=txtDienThoai.Text;
                Searched(this, new SearchEventArgs(ten, loaiCongNhan, namSinh, diaChi, dienThoai));
            }
        }
    }
    public class SearchEventArgs : EventArgs
    {
        public string Ten { set; get; }
        public LoaiCongNhan ChucVu { set; get; }
        public int NamSinh { set; get; }
        public string DiaChi { set; get; }
        public string DienThoai { set; get; }

        public SearchEventArgs(string ten, LoaiCongNhan loaiCongNhan, int namSinh, string diaChi, string dienThoai)
        {
            Ten = ten;
            ChucVu = loaiCongNhan;
            NamSinh = namSinh;
            DiaChi = diaChi;
            DienThoai = dienThoai;
        }
    }
}
